﻿// License: https://opensource.org/licenses/MIT
// Create Date : 2018-01-01

using System;
using Cat.Core.XLog;

namespace Cat.Core
{
    /// <summary>
    /// Log提供者静态类
    /// </summary>
    public static class Log
    {
        static ILogProvider _provider;
        /// <summary>
        /// 若要使用Log功能，需要设置一个Provider
        /// </summary>
        public static ILogProvider Provider
        {
            get { return _provider; }
            set
            {
                if (_provider != value)
                {
                    _provider = value;
                    if (_provider != null) _defaultLog = _provider.GetLogger(name: null);
                    else _defaultLog = null;
                }
                if (value == null) _defaultLog = null;
            }
        }
        /// <summary>
        /// 不做任何操作的ILogger
        /// </summary>
        public static ILogger Null { get { return NullLogger.Instance; } }
        static ILogger _defaultLog;
        /// <summary>
        /// 默认日志
        /// </summary>
        public static ILogger Default { get { return _defaultLog ?? Log.Null; } }

        #region GetLogger
        /// <summary>
        /// 从Provider上取一个ILogger
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static ILogger Get(Type type)
        {
            if (Provider == null) return Null;
            return Provider.GetLogger(type) ?? Null;
        }
        /// <summary>
        /// 从Provider上取一个ILogger
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static ILogger Get(string name)
        {
            if (Provider == null) return Null;
            return Provider.GetLogger(name) ?? Null;
        }
        #endregion

    }
}
